/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:

    int dfs(TreeNode* root, int cnt)
    {
        if (root->left == nullptr && root->right == nullptr)
            return cnt * 10 + root->val;


        int ret1 = root->left == nullptr ? 0 : dfs(root->left, cnt * 10 + root->val);
        int ret2 = root->right == nullptr ? 0 : dfs(root->right, cnt * 10 + root->val);

        return ret1 + ret2;
    }


    int sumNumbers(TreeNode* root)
    {
        return dfs(root, 0);


    }
};